package com.ray.ba.security.app.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ray.ba.starter.security.jwt.security.JwtUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author r.shi 2020/03/30 1:33
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SecUserDto {
    @NotNull(groups = Update.class)
    private Long id;

    @NotBlank(message = "用户名不能为空")
    private String username;

    @NotBlank(message = "邮箱不能为空")
    @Email(message = "邮箱格式不正确")
    private String email;

    private Boolean sex;

    private Integer age;

    private Boolean userStatus;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    private List<String> roles;


    public SecUserDto(JwtUser jwtUser){
        this.id = jwtUser.getId();
        this.email = jwtUser.getEmail();
        this.username = jwtUser.getUsername();
        this.roles = jwtUser.getAuthorities().stream().map(authority ->authority.getAuthority().toLowerCase() ).collect(Collectors.toList());
    }

    public @interface Update {}

}
